package zjf.vo;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
public class MD5Util {
	private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));

		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
		"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	//http://mch.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3
//		private static String Key =WeixinContons.APPSECRET;

		/**
		 * @param args
		 */
//		public static void main(String[] args) {
//			System.out.println(">>>模拟微信支付<<<");
//			System.out.println("==========华丽的分隔符==========");
//			//微信api提供的参数
////			String appid = "wxf8471ff43eccfa90";
////			String body = "jufugou";
//			String ip="10.10.114.92";
//			String money="0.01";
//			String openId="oEollt47lV--uNeThDZymRkQf2ew";
////			String goods_id="123456789";
//			
//			SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
//			parameters.put("appid", WeixinContons.APPID);
//			parameters.put("body","zdgshaokao");
//			parameters.put("detail","zdgshaokao");
//			parameters.put("device_info","WEB");//设备号
//			parameters.put("fee_type","CNY");
//			parameters.put("mch_id",WeixinContons.BUSINESSNO);//商户号
//			String notify_url="http://zhongjufu123.com/";
//			
////			String notify_url="http://zhongjufu123.com/weChatpay/notifyServlet?info="+openId+"|"+goods_id;
////			String notify_url="http://jufugou123.com/weChatpay/notifyServlet?info="+openId+"|"+goods_id;
//			System.out.println(notify_url);
//			parameters.put("nonce_str","c91fbfb223c4487795d07c85d7d7d4a5");
//			parameters.put("notify_url",notify_url);
//			parameters.put("openid",openId);
//			parameters.put("out_trade_no","2016030423144062699");
//			parameters.put("spbill_create_ip",ip);//用户端ip
//			parameters.put("total_fee","0.01");
//			parameters.put("trade_type","JSAPI");
//			
////			String characterEncoding = "GBK";
//			String characterEncoding = "UTF-8";
//			String weixinApiSign = "6CD95DAA7E14ADC5D9877AFBF08BA17C";
//			System.out.println("微信的签名是：" + weixinApiSign);
//			String mySign = createSign(characterEncoding,parameters,Key);
//			System.out.println("我     的签名是："+mySign);
//			
//			if(weixinApiSign.equals(mySign)){
//				System.out.println("恭喜你成功了~");
//			}else{
//				System.out.println("注定了你是个失败者~");
//			}
//			
////			String userAgent = "Mozilla/5.0(iphone;CPU iphone OS 5_1_1 like Mac OS X) AppleWebKit/534.46(KHTML,like Geocko) Mobile/9B206 MicroMessenger/5.0";
////			
////			char agent = userAgent.charAt(userAgent.indexOf("MicroMessenger")+15);
////			
////			System.out.println("微信的版本号："+new String(new char[]{agent}));
//		}

		/**
		 * 微信支付签名算法sign
		 * @param characterEncoding
		 * @param parameters
		 * @return
		 */
		@SuppressWarnings("unchecked")
		public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters,String key){
			StringBuffer sb = new StringBuffer();
			Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
			Iterator it = es.iterator();
			while(it.hasNext()) {
				Map.Entry entry = (Map.Entry)it.next();
				String k = (String)entry.getKey();
				Object v = entry.getValue();
				if(null != v && !"".equals(v) 
						&& !"sign".equals(k) && !"key".equals(k)) {
					sb.append(k + "=" + v + "&");
				}
			}
			sb.append("key=" + key);
			String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
			return sign;
		}
}
